package com.csit.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.entity.User;
import com.csit.service.UserService;
import com.csit.util.MD5Util;
import com.csit.util.ResponseMsgUtil;

@ParentPackage("basePackage")
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = -1379893197712674493L;
	
	private String userCode;
	
	private String password;
	
	@Resource
	private UserService userService;
	
	public void logon(){
		try {
			ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
			User user = userService.getEntity("userCode", userCode);
			if(user==null){
				msgUtil.setMsg("用户不存在");
				ajaxJson(msgUtil.toJSON());
				return;
			}
			if(!user.getPassword().equals(MD5Util.digest(password))){
				msgUtil.setMsg("密码错误");
				ajaxJson(msgUtil.toJSON());
				return;
			}
			setSession(User.LOGINID, user.getUserID());
			setSession(User.LOGINTYPE, user.getRoleType());
			setSession(User.LOGINNAME, user.getUserName());
			setSession(User.USERSHOP,user.getShopID());
			msgUtil.setSuccess(true);
			ajaxJson(msgUtil.toJSON());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void logout(){
		getSession().clear();
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
